package com.ldg.crm.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Formatter;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.ldg.crm.common.MyConverter;
import com.ldg.crm.dao.EmployeeDao;
import com.ldg.crm.dao.EmployeeEntitySqlProvider;
import com.ldg.crm.dao.UserDao;
import com.ldg.crm.dto.EmployeeDto;
import com.ldg.crm.dto.UserDto;
import com.ldg.crm.entity.BaseEntity;
import com.ldg.crm.entity.BaseEntityExample;
import com.ldg.crm.entity.DepartmentEntityExample;
import com.ldg.crm.entity.EmployeeEntity;
import com.ldg.crm.entity.EmployeeEntityExample;
import com.ldg.crm.entity.EmployeeEntityExample.Criteria;
import com.ldg.crm.entity.UserEntity;
import com.ldg.crm.entity.UserEntityDto;
import com.ldg.crm.entity.UserEntityExample;
import com.ldg.crm.service.EmployeeService;
import com.ldg.crm.util.Generator;

@Service
@Transactional
public class EmployeeServiceImp implements EmployeeService {

	@Autowired
	EmployeeDao employeeDao;
	@Autowired
	UserDao userDao;

	@Override
	public int addEmployee(EmployeeEntity emp) {

		return employeeDao.insert(emp);
	}

	@Override
	public int updateEmployee(EmployeeEntity employ) {

		EmployeeEntityExample record = new EmployeeEntityExample();

		return employeeDao.updateByPrimaryKeySelective(employ);
	}

	@Override
	public int deleteEmployee(Integer[] ids) {

		EmployeeEntityExample example = new EmployeeEntityExample();

		// 条件对像 where
		Criteria criteria = example.createCriteria();

		// 添加一个条件 employeeId in ( 集合 )
		criteria.andEmployeeIdIn(Arrays.asList(ids));

		return employeeDao.deleteByExample(example);
	}

	// String searchFactor,
	@Override
	public EmployeeDto findPageList1(String queryName, java.util.Date hireDate, int pageNum, int pageSize) {
		// 返回的视图层的数据

		EmployeeDto result = new EmployeeDto();

		// 分页插件实现
		PageHelper.startPage(pageNum, pageSize);

		// Mybatis自动生成的示例类
		EmployeeEntityExample example = new EmployeeEntityExample();

		// 条件对像 where
		Criteria criteria = example.createCriteria();

//				//department 部门 position 职位  name姓名 sex性别
//				if(searchFactor.equals("department")) {
//					//select departmentId from crm_department where name = 'queryName';
//					
//				}else if(searchFactor.equals("position")) {
//					
//				}else if(searchFactor.equals("name")) {
//					
//				}else if(searchFactor.equals("sex")) {
//					
//				}else {
//					
//				}

		// 查询用户名
		if (queryName != null) {
			criteria.andNameLike("%" + queryName + "%");
			System.out.println("查询用户：" + queryName + "-----");
		}

		if (hireDate != null) {
			criteria.andHireDateGreaterThan(hireDate);
			System.out.print("查询入职日期：" + hireDate + "-----");
		}

		// System.out.println("查询条件：" + queryName + "--------" + hireDate + "--------" +
		// termDate);

		// 得到当前页的数据
		List<EmployeeEntity> rows = employeeDao.selectByExample(example);

		EmployeeEntitySqlProvider em = new EmployeeEntitySqlProvider();
		System.out.println(em.selectByExample(example));

		// 得到总的记录条数
		Long countNumber = employeeDao.countByExample(example);

		if (rows.size() > 0) {
			result = new EmployeeDto();
			result.setRows(rows);
			result.setTotal(countNumber);
		} else {
			result = new EmployeeDto();
			result.setRows(null);
			result.setTotal(0);
		}

		return result;

	}

	@Override
	public int deleteUser(Integer[] ids) {
		UserEntityExample example = new UserEntityExample();

		// 条件对像 where
		com.ldg.crm.entity.UserEntityExample.Criteria criteria = example.createCriteria();
		// 添加一个条件 Uid in ( 集合 )
		criteria.andUidIn(Arrays.asList(ids));

		return userDao.deleteByExample(example);
	}

	@Override
	public UserDto findPageList1(int pageNum, int pageSize) {
		// 返回的视图层的数据

//		System.out.println(pageNum+"--------"+pageSize);
		UserDto result = null;

		// 分页插件实现
		PageHelper.startPage(pageNum, pageSize);

		// Mybatis自动生成的示例
		UserEntityExample example = new UserEntityExample();

		// 得到当前页的数据
		List<UserEntityDto> rows = userDao.selectByExample_test();

		// 得到总的记录条数
		Long countNumber = userDao.countByExample(example);

//				System.out.println(rows+"--------"+countNumber);

		if (rows.size() > 0) {
			result = new UserDto();
			result.setRows(rows);
			result.setTotal(countNumber);
		}

		return result;
	}

	@Override
	public int addUser(UserEntity user) {
		return userDao.insert(user);
	}

	@Override
	public int updateUser(UserEntity user) {
		UserEntityExample record = new UserEntityExample();

		return userDao.updateByPrimaryKeySelective(user);
	}

	@Override
	public UserEntity findByNumberAndPassword(UserEntity user) {
		// TODO Auto-generated method stub

		return userDao.findByNumberAndPassword(user);
	}

	/*
	 * @param employeeId 这是员工ID employeeId()
	 * 
	 * @param departmentId 这是所在部门 depId()
	 * 
	 * @param sid 这是身份证ID sid()
	 */
	@Override
	public int createUser(EmployeeEntity employ) {
		UserEntity user = new UserEntity();
		EmployeeEntity emp = new EmployeeEntity();
		
		Integer employeeId = employ.getEmployeeId();
		Integer departmentId = employ.getDepartmentId();
//		String name = employ.getName();
		String sid = employ.getSid();

		if (employeeId != null || employeeId.equals("")) {
			user.setEmployeeId(employeeId);
		}
		if (departmentId != null || departmentId.equals("")) {
			user.setDepId(departmentId);
		}
		if (sid != null || sid.equals("")) {
			String pwd = Generator.getSidLastSix(sid);
			user.setUserName(sid);
			user.setUserPwd(pwd);
		}
//		if (name != null || name.equals("")) {
//			emp.setName(name);
//		}
		user.setCreateDate(new Date());
		user.setStatus((byte) 2);//停用状态

		// 校验数据是否存在
		UserEntity i = userDao.selectByPrimaryKey(employeeId);
		int userExit = userDao.selectByUserName(sid);
		if (i != null || userExit!=0 ) {
			return 0;
		} else {
			return userDao.insert(user);
		}

	}

}
